﻿using UnityEngine;
using System.Collections;

public enum WalkDirection{
	south = 1,
	west = 2,
	north = 3,
	east = 4,
	southwest = 5,
	northwest = 6,
	northeast = 7,
	southeast = 8
}


public class NPCController : MonoBehaviour {

	public bool isAttackable;
	public bool doesWander;
	public bool isMoving;
	public WalkDirection direction;

	private Vector3 finaltransform;
	private float lastupdate;

	public 
	// Use this for initialization
	void Start () {
	
	}

	// Update is called once per frame
	void Update () {
		if (!isMoving && doesWander) {
			direction = (WalkDirection)Random.Range (1, 5);

			gameObject.GetComponent<Animator>().SetInteger("Direction", (int)direction);

			Vector3 movement = Vector3.zero;

			switch(direction){
				case WalkDirection.east:
					movement = new Vector3(1, 0, 0);
					break;
				case WalkDirection.west:
					movement = new Vector3(-1, 0, 0);
					break;
				case WalkDirection.north:
					movement = new Vector3(0, 0, 1);
					break;
				case WalkDirection.south:
					movement = new Vector3(0, 0, -1);
					break;
			}


			finaltransform = this.transform.position + movement;
			lastupdate = Time.time;
			isMoving = true;
				
		}

		if (isMoving) {
						this.transform.position = Vector3.Lerp (this.transform.position, finaltransform, Mathf.Clamp((float)(Time.time - lastupdate), 0.0f, 1.0f));

						if (this.transform.position == finaltransform){
								isMoving = false;
								
								finaltransform = Vector3.zero;

							}
				}


	
	}
}
